import requests,re
#https://wkretype.bdimg.com/retype/text/cbb4af8b783e0912a3162a89?md5sum=6e7a10b16f3ad8d3b40ecd0dfe8d1b67&sign=4f15488451&callback=cb&pn=1&rn=4&type=txt&rsign=p_4-r_0-s_460f0&_=1590110466517
#https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id=cbb4af8b783e0912a3162a89&t=1590110666705&_=1590110666430
session=requests.session()
def fetch_url(url):
    # print(session.get(url).content.decode('gbk'))
    return session.get(url).content.decode('gbk')

def get_doc_id(url):
    #https://wenku.baidu.com/view/cbb4af8b783e0912a3162a89.html?from=search
    return re.findall('view/(.*).html',url)[0]

def parser_type(content):
    return re.findall(r"docType.*?\:.*?\'(.*?)\'\,",content)[0]#返回一个列表，列表里只有一个值

def parser_title(content):
    return re.findall(r"title.*?\:.*?\'(.*?)\'\,",content)[0]

def parser_txt(doc_id):
    content_url='https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id='+doc_id
    content=fetch_url(content_url)
    md5=re.findall('"md5sum":"(.*?)"',content)[0]
    print(md5)
    pn=re.findall('"totalPageNum":"(.*?)"',content)
    print(pn)

def main():
    url=input('请输入要下载的文库url地址')
    #请求
    content=fetch_url(url)
    #获取id
    doc_id=get_doc_id(url)
    print(doc_id)
    #文档类型
    type=parser_type(content)
    print(type)
    #文档标题
    title=parser_title(content)
    print(title)
    if type=='txt':
        parser_txt(doc_id)


main()
a=['alex','engo','alw']
s='_'.join(a)
print(s)
s=''
for i in a:
   s=s+i+'_'
print(s.strip('_'))